//
//  TouchMoveSingleView.m
//  万物记
//
//  Created by Ajen on 2019/6/15.
//  Copyright © 2019 Ajen. All rights reserved.
//

#import "TouchMoveSingleView.h"
#import "UIView+Extension.h"
@implementation TouchMoveSingleView{
    UIPanGestureRecognizer * panGesRecognizer;
    UIView * superView;
    CGPoint lastMovedPoint;//松手之前的位置
    UIView * surroundView;
    UIImageView * img;
}

- (instancetype)initWithFrame:(CGRect)frame superView:(UIView *)sView
{
    self = [super initWithFrame:frame];
    if (self) {
        superView = sView;
        
        
        
        self.aj_cornerRadius(25).aj_borderColorHexStr(@"#2d2e36").aj_borderWidth(3);
        self.layer.masksToBounds = NO;
        [self shadow:[UIColor blackColor] opacity:1 radius:5 offset:CGSizeMake(0, 0)];
        img = [UIImageView new];
        img.aj_imgName(@"ss").aj_addToView(self);
        img.userInteractionEnabled = YES;
        [img mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(self).offset(8);
            make.right.bottom.equalTo(self).offset(-8);
        }];
        
        surroundView = [UIView new];
        surroundView.aj_cornerRadius(25).aj_borderColorHexStr(@"#2d2e36").aj_borderWidth(3).aj_addToView(self);
        surroundView.userInteractionEnabled = YES;
        [surroundView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.equalTo(@50);
            make.center.equalTo(self);
        }];
        
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self animation];
        });
        
        UIView * panView = [UIView new];
        [self addSubview:panView];
        [panView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        panGesRecognizer= [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(onPanGesRecognizer:)];
        [self addGestureRecognizer:panGesRecognizer];
    }
    return self;
}

-(void)animation{
    //模仿呼吸节奏动画,采用递归方式,用1.3秒扩张到最大,模仿吸气动作,扩张到最大停顿0.3秒,模仿人类吸满气后的停顿,然后再用1.3秒缩回原始状态,模仿呼气,缩到原始状态后模仿人类呼吸停顿1秒,然后递归
    [surroundView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(@75);
    }];
    [UIView animateWithDuration:1.3 animations:^{
        [surroundView.superview layoutIfNeeded];
        surroundView.aj_cornerRadius(37.5).aj_borderWidth(1);
    } completion:^(BOOL finished) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [surroundView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.height.equalTo(@50);
            }];
            [UIView animateWithDuration:1.3 animations:^{
                [surroundView.superview layoutIfNeeded];
                surroundView.aj_cornerRadius(25).aj_borderWidth(1);
            } completion:^(BOOL finished) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self animation];
                });
            }];
        });
    }];
}


- (void)onPanGesRecognizer:(UIPanGestureRecognizer*)ges {
    CGPoint offset = [ges translationInView:superView];
    CGPoint center = CGPointMake(self.center.x+offset.x, self.center.y+offset.y);
    if(ges.state== UIGestureRecognizerStateChanged) {
        if(center.x<= 35) {
            center.x= 35;
        } else if(center.x>= superView.bounds.size.width-35) {
            center.x= superView.bounds.size.width-35;
        }
        if(center.y<= 45) {
            center.y= 45;
        } else if(center.y>= superView.bounds.size.height-45) {
            center.y= superView.bounds.size.height-45;
        }
        [self setCenter:center];
        
        //设置位置
        [ges setTranslation:CGPointMake(0, 0) inView:superView];
        lastMovedPoint = CGPointMake(self.CenterX, self.CenterY);
        if(self.moveChangeBlock){
            self.moveChangeBlock(lastMovedPoint);
        }
    }
    
    if(ges.state== UIGestureRecognizerStateEnded){
        if(!self.stayPoint.x){//未设置悬停位置
            [UIView animateWithDuration:0.2 animations:^{
                self.CenterX = superView.Width - 45;
                self.CenterY = superView.Height - superView.Height * 0.35 + 25;
            }];
            if(self.moveEndBlock){
                self.moveEndBlock(lastMovedPoint);
            }
        }else{
            [UIView animateWithDuration:0.2 animations:^{
                self.CenterX = self.stayPoint.x;
                self.CenterY = self.stayPoint.y;
            }];
            if(self.moveEndBlock){
                self.moveEndBlock(lastMovedPoint);
            }
        }
        
    }
}


-(void)setIconImage:(UIImage *)iconImage{
    _iconImage = iconImage;
    img.image = iconImage;
}
@end
